<?php
/**
 * User:chendenan
 * Created On 2022/3/9 0009 21:15
 */

namespace app\api\exception;

use think\exception\Handle;
use think\Response;
use Throwable;

class Http extends Handle {
    public $httpStatus = 500 ;

    /**
     * Render an exception into an HTTP response
     * @param $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request , Throwable $e) :Response{
        if($e instanceof \think\Exception){
            return show($e->getMessage(),$e->getCode());
        }
        // 请求异常
        if($e instanceof \think\Exception\HttpResponseException){
            return parent::render($request, $e);
        }

        if(method_exists($e,"getStatusCode")){
            $httpStatus = $e->getStatusCode;
        }else{
            $httpStatus = $this->httpStatus;
        }
        //添加自定义异常处理机制
        return show($e->getMessage(),config("status.error"),[],$httpStatus);
    }
}